package vip.xiaonuo.issued.impl;

import org.springframework.stereotype.Service;
import vip.xiaonuo.core.exception.ServiceException;
import vip.xiaonuo.facedoor.modular.devicebase.entity.DeviceBase;
import vip.xiaonuo.facedoor.modular.devicebase.service.DeviceBaseService;
import vip.xiaonuo.facedoor.modular.memberdevice.constant.MemberDeviceConstant;
import vip.xiaonuo.facedoor.modular.visitor.entity.Visitor;
import vip.xiaonuo.facedoor.modular.visitor.mapper.VisitorMapper;
import vip.xiaonuo.facedoor.modular.visitordevice.entity.VisitorDevice;
import vip.xiaonuo.facedoor.modular.visitordevice.mapper.VisitorDeviceMapper;
import vip.xiaonuo.facedoor.iservice.IVisitorDeviceService;
import vip.xiaonuo.facedoor.rule.HQRulesEngine;
import vip.xiaonuo.facedoor.rule.RuleConstant;
import javax.annotation.Resource;

@Service(value = RuleConstant.HQ+ RuleConstant.VISITOR_DEVICE)
public class HQVisitorDeviceServiceImpl implements IVisitorDeviceService {

  @Resource
  private VisitorMapper visitorMapper;

  @Resource
  private VisitorDeviceMapper visitorDeviceMapper;

  @Resource
  private DeviceBaseService deviceBaseService;

  private void baseCheck(VisitorDevice visitorDevice) {
    if (visitorDevice == null) {
      throw new ServiceException(500, "找不到关联信息！");
    }
    if (visitorDevice.getStatus().equals(MemberDeviceConstant.ADD) || visitorDevice.getStatus().equals(MemberDeviceConstant.UPDATE) || visitorDevice.getStatus().equals(MemberDeviceConstant.DELETE)) {
      throw new ServiceException(500, "非正常状态 不允许操作！");
    }
  }


  @Override
  public void delete(Long id) throws Exception {
    VisitorDevice visitorDevice = visitorDeviceMapper.selectById(id);
    baseCheck(visitorDevice);
    DeviceBase deviceBase = deviceBaseService.getById(visitorDevice.getDeviceId());
    Visitor visitor = visitorMapper.selectById(visitorDevice.getVisitorId());
    // 新增失败 直接删除人员
    if (visitorDevice.getStatus().equals(MemberDeviceConstant.ADD_FAIL)) {
      visitorDeviceMapper.deleteById(id);
    } else {
      HQRulesEngine.visitorDealWithService.delete(visitorDevice, visitor, null, deviceBase);

    }
  }

  @Override
  public void sync(Long id) throws Exception {
    VisitorDevice visitorDevice = this.visitorDeviceMapper.selectById(id);
    baseCheck(visitorDevice);
    if (visitorDevice.getStatus().equals(MemberDeviceConstant.SYNC_SUCCESS)) {
      throw new ServiceException(500, "未发生错误 不允许同步！");
    }
    DeviceBase deviceBase = deviceBaseService.getById(visitorDevice.getDeviceId());
    Visitor visitor = visitorMapper.selectById(visitorDevice.getVisitorId());
    // 恢复人员状态
    if (visitorDevice.getStatus().equals(MemberDeviceConstant.ADD_FAIL)) {
      HQRulesEngine.visitorDealWithService.add(visitorDevice,visitor,null,deviceBase);
    }
    if (visitorDevice.getStatus().equals(MemberDeviceConstant.UPDATE_FAIL)) {
      HQRulesEngine.visitorDealWithService.update(visitorDevice, visitor, null, deviceBase);
    }
    if (visitorDevice.getStatus().equals(MemberDeviceConstant.DELETE_FAIL)) {
      HQRulesEngine.visitorDealWithService.delete(visitorDevice, visitor, null, deviceBase);
    }
    visitorDeviceMapper.updateById(visitorDevice);
  }
}
